/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/
#include "scpersistenceservice.h"

ScPersistenceService::ScPersistenceService(Lesson& lesson,std::string path)
: chatStream(0),
  scStream(0)
{
  savePath=path+lesson.getName();
}
bool ScPersistenceService::isAvailable(){
  std::string pathIfExist=savePath+"_sc";
  std::fstream f(pathIfExist.c_str());
  return f;
}
void ScPersistenceService::openForWriting(){
  savePath+="_sc";
   scStream=new std::fstream(savePath.c_str(),std::ios::out | std::ios::binary | std::ios::app);
   savePath+="_chat";
   chatStream=new std::fstream(savePath.c_str(),std::ios::out | std::ios::binary | std::ios::app);
}
void ScPersistenceService::openForReading(){
  savePath+="_sc";
  scStream=new std::fstream(savePath.c_str(),std::ios::in |  std::ios::binary);
  savePath+="_chat";
  chatStream=new std::fstream(savePath.c_str(),std::ios::in | std::ios::binary);
}


ScPersistenceService::~ScPersistenceService()
{
  if(chatStream)
    delete chatStream;
  if(scStream)
    delete scStream;
}

void ScPersistenceService::persistChatMessage(char* data,int size)
{
  chatStream->write((const char*)data,size);
}

void ScPersistenceService::persistScreenCastData(char* data,int size)
{
  scStream->write((const char*)data,size);
}


void  ScPersistenceService::sendSavedScreencast(BlockingQueue& queue)
{
  sendStreamData(chatStream,queue);
  sendStreamData(scStream,queue);
}

void  ScPersistenceService::sendStreamData(std::fstream* stream,BlockingQueue& queue)
{

  char header[5];
  while(stream->read(header,sizeof(header))){
  unsigned int tamanio = 0;
  tamanio = (tamanio << 8)+(int) ((unsigned char) header[4]);
  tamanio = (tamanio << 8)+(int) ((unsigned char) header[3]);
  tamanio = (tamanio << 8)+(int) ((unsigned char) header[2]);
  tamanio = (tamanio << 8)+(int) ((unsigned char) header[1]);

  tamanio+=5;
  std::vector<char> data(tamanio);
  data[0]=header[0];
  data[1]=header[1];
  data[2]=header[2];
  data[3]=header[3];
  data[4]=header[4];
  stream->read(&data.at(5),tamanio-5);
  queue.push(data);
  }
}


